﻿using Microsoft.EntityFrameworkCore;
using RepositoryUnit.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace RepositoryUnit.Repository
{
    public class EFRepository<TEntity, TDbContext> : IRepository<TEntity> where TEntity : class
                                                                          where TDbContext : DbContext
    {
        private readonly TDbContext _context;
        private readonly DbSet<TEntity> _dbSet;

        public EFRepository(TDbContext context)
        {
            _context = context;
            _dbSet = _context.Set<TEntity>();
        }

        public IQueryable<TEntity> Queryable => _dbSet.AsQueryable();

        public async Task AddAsync(TEntity entity)
        {
            await _dbSet.AddAsync(entity);
        }

        public async Task AddRangeAsync(List<TEntity> list)
        {
            await _dbSet.AddRangeAsync(list);
        }

        public async Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await _dbSet.Where(predicate).CountAsync();
        }

        public async Task<bool> ExistAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await _dbSet.AnyAsync(predicate);
        }

        public async Task<IEnumerable<TEntity>> GetAllListAsync()
        {
            return await _dbSet.ToListAsync();
        }

        public async Task<IEnumerable<TEntity>> GetListAsync(IQueryable<TEntity> queryable)
        {
            return await queryable.ToListAsync();
        }

        public async Task<IEnumerable<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> expression)
        {
            return await _dbSet.Where(expression).ToListAsync();
        }

        public async Task<IEnumerable<TEntity>> GetAllListWithNoTrackingAsync()
        {
            return await _dbSet.AsNoTracking().ToListAsync();
        }

        public async Task<IEnumerable<TEntity>> GetListWithNoTrackingAsync(IQueryable<TEntity> queryable)
        {
            return await queryable.AsNoTracking().ToListAsync();
        }

        public async Task<IEnumerable<TEntity>> GetListWithNoTrackingAsync(IQueryable<TEntity> queryable, int page, int pageSize)
        {
            return await queryable.AsNoTracking().Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        public async Task<IEnumerable<TEntity>> GetListWithNoTrackingAsync(Expression<Func<TEntity, bool>> expression)
        {
            return await _dbSet.Where(expression).AsNoTracking().ToListAsync();
        }

        public async Task<IEnumerable<TEntity>> GetListWithNoTrackingAsync(Expression<Func<TEntity, bool>> expression, int page, int pageSize)
        {
           return await _dbSet.Where(expression).AsNoTracking().Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        public async Task<TEntity> GetModelAsync(Expression<Func<TEntity, bool>> predicate)
        {
            return await _dbSet.Where(predicate).FirstOrDefaultAsync();
        }

        public async Task<TEntity> GetModelByIdAsync(object id)
        {
            return await _dbSet.FindAsync(id);
        }

        public void Remove(TEntity entity)
        {
            _dbSet.Remove(entity);
        }

        public void RemoveRange(List<TEntity> list)
        {
            _dbSet.RemoveRange(list);
        }

        public void Update(TEntity entity)
        {
            _dbSet.Update(entity);
        }

        public void UpdateRange(List<TEntity> list)
        {
            _dbSet.UpdateRange(list);
        }
    }
}
